package com.harmonyos.zxing;

import com.google.zxing.journeyapps.barcodescanner.BusHelper;
import com.google.zxing.journeyapps.barcodescanner.CaptureManager;
import com.google.zxing.journeyapps.barcodescanner.DecoratedBarcodeView;
import com.google.zxing.journeyapps.barcodescanner.ViewfinderView;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.window.service.Window;
import ohos.agp.window.service.WindowManager;
import ohos.global.resource.NotExistException;
import ohos.global.resource.ResourceManager;
import ohos.global.resource.WrongTypeException;
import ohos.utils.PacMap;

import java.io.IOException;
import java.security.SecureRandom;

public class CustomScannerAbility extends Ability implements DecoratedBarcodeView.TorchListener, BusHelper.Observable {

    private CaptureManager capture;
    private DecoratedBarcodeView barcodeScannerView;
    private Button switchFlashlightButton;
    private ViewfinderView viewfinderView;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_ability_custom_scanner);
        WindowManager windowManager = WindowManager.getInstance();
        Window window = windowManager.getTopWindow().get();
        window.setTransparent(true);

        barcodeScannerView = (DecoratedBarcodeView) findComponentById(ResourceTable.Id_zxing_barcode_scanner);
        barcodeScannerView.setTorchListener(this);

        switchFlashlightButton = (Button) findComponentById(ResourceTable.Id_switch_flashlight);

        viewfinderView = (ViewfinderView) findComponentById(ResourceTable.Id_zxing_viewfinder_view);

        if (!hasFlash()) {
            switchFlashlightButton.setVisibility(Component.HIDE);
        }

        capture = new CaptureManager(this, barcodeScannerView);
        capture.initializeFromIntent(getIntent(), null);
        capture.setShowMissingCameraPermissionDialog(false);
//      capture.decode();

        changeMaskColor(null);
        changeLaserVisibility(true);
        BusHelper.getINSTANCE().register("CameraPreviewStarted", this);
    }

    @Override
    protected void onActive() {
        super.onActive();
        capture.onResume();
    }

    @Override
    protected void onInactive() {
        super.onInactive();
        capture.onPause();
    }

    @Override
    public void onSaveAbilityState(PacMap outState) {
        super.onSaveAbilityState(outState);
        capture.onSaveInstanceState(outState);
    }

    @Override
    public void onRestoreAbilityState(PacMap inState) {
        super.onRestoreAbilityState(inState);
        capture.initializeFromIntent(getIntent(), inState);
        capture.setShowMissingCameraPermissionDialog(false);
        capture.decode();
    }

    @Override
    protected void onStop() {
        super.onStop();
        BusHelper.getINSTANCE().unRegister(this);
        capture.onDestroy();
    }

    private boolean hasFlash() {
        // to implement if support flashlight
        return true;
    }

    public void switchFlashlight(Component view) {
        ResourceManager resourceManager = getResourceManager();
        String string = null;
        try {
            string = resourceManager.getElement(ResourceTable.String_turn_on_flashlight).getString();
            if (string.equals(switchFlashlightButton.getText())) {
                barcodeScannerView.setTorchOn();
            } else {
                barcodeScannerView.setTorchOff();
            }
        } catch (IOException | NotExistException | WrongTypeException e) {
            e.printStackTrace();
        }
    }

    public void changeMaskColor(Component view) {
        SecureRandom rnd = new SecureRandom();
        int color = Color.argb(100, rnd.nextInt(256), rnd.nextInt(256), rnd.nextInt(256));
        viewfinderView.setMaskColor(color);
    }

    public void changeLaserVisibility(boolean visible) {
        viewfinderView.setLaserVisibility(visible);
    }

    @Override
    public void onTorchOn() {
        switchFlashlightButton.setText(ResourceTable.String_turn_off_flashlight);
    }

    @Override
    public void onTorchOff() {
        switchFlashlightButton.setText(ResourceTable.String_turn_on_flashlight);
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        capture.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public void onSubscribe(Object object) {
        if (capture != null)
            capture.decode();
    }
}
